package org.smile.db;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.smile.db.dao.ConfigContext;
import org.smile.db.dao.impl.BaseDAOImpl;
import org.smile.db.dao.impl.BaseVo;
import org.smile.db.handler.FieldBeanRowHandler;
import org.smile.db.sql.ParamParser;
import org.smile.db.sql.SQLRunner;
import org.smile.db.sql.TableInfo;
/**
 * 一个实现基本功能的数据库访问对象
 * @author strive
 *
 */
public class JdbcTemplate<E extends BaseVo> extends BaseDAOImpl<E>{
	
	
	private ConfigContext config=ConfigContext.getInstance();
	
	public JdbcTemplate(){
	}
	public JdbcTemplate(DataSource ds){
		super(ds);
	}
	/**
	 * 保存一个map对象到一个表中
	 * map中的key对应表中的字段
	 * @param tableName 表名
	 * @param map 名称字段
	 * @throws Exception
	 */
	public void add(Class<E> c,Map<String,Object> map) throws SQLException{
		TableInfo info=new TableInfo(config.getTableName(c));
		info.setFields(map);
		info.setKeyField(config.getIdCollmun(config.getIdField(c)));
		ParamParser params=info.getInsertSQLParams();
		DbUtils.execute(dataSource.getConnection(),params.getSql(),params.getParams());
	}
	/**
	 * 更新一个map对象到表中 
	 * @param tableName 表名
	 * @param map 所有的字段值
	 * @param keys 主键字段
	 * @throws Exception 
	 */
	public void update(Class<E>  clazz,Map map) throws SQLException{
		TableInfo info=new TableInfo(config.getTableName(clazz));
		info.setFields(map);
		info.setKeyField(config.getIdCollmun(config.getIdField(clazz)));
		ParamParser params=info.getUpdateSQLParams();
		DbUtils.execute(dataSource.getConnection(),params.getSql(),params.getParams());
	}
	/**
	 * 查询对象
	 * @param c
	 * @param whereSql 过滤条件
	 * @return
	 * @throws Exception
	 */
	public List<E> findAll(Class<E> c,String whereSql) throws SQLException{
		TableInfo info=new TableInfo(config.getTableName(c));
		info.setFilterStr(whereSql);
		SQLRunner tool=null;
		try{
			tool=new SQLRunner(dataSource.getConnection(),new FieldBeanRowHandler(c));
			ParamParser parser=info.getSelectSQLParams();
			List<E> list=tool.query(parser.getSql(),parser.getParams());
			return list;
		}finally{
			tool.closeConn();
		}
	}
	/**
	 * 查询对象
	 * @param c
	 * @param whereSql 过滤条件
	 * @return
	 * @throws Exception
	 */
	public List<E> findAll(Class<E> c) throws SQLException{
		return findAll(c,null);
	}
	/**
	 * 查询对象
	 * @param c
	 * @param whereSql 过滤条件
	 * @return
	 * @throws Exception
	 */
	public List<E> find(Class<E> c,String whereSql,String orderStr) throws SQLException{
		TableInfo info=new TableInfo(config.getTableName(c));
		info.setFilterStr(whereSql);
		info.setOrderStr(orderStr);
		SQLRunner tool=null;
		try{
			tool=new SQLRunner(dataSource.getConnection(),new FieldBeanRowHandler(c));
			ParamParser parser=info.getSelectSQLParams();
			List<E> list=tool.query(parser.getSql(),parser.getParams());
			return list;
		}finally{
			tool.closeConn();
		}
	}
	/**
	 * 查询对象
	 * @param c
	 * @param whereSql 过滤条件
	 * @return
	 * @throws Exception
	 */
	public PageModel<E> queryPageSql(Class<E> c,String whereSql,int page,int size,int dialect) throws Exception{
		TableInfo info=new TableInfo(config.getTableName(c));
		info.setFilterStr(whereSql);
		SQLRunner tool=null;
		try{
			tool=new SQLRunner(dataSource.getConnection(),new FieldBeanRowHandler(c));
			tool.setDbDialect(dialect);
			ParamParser parser=info.getSelectSQLParams();
			PageModel<E> pageModel=tool.queryPageSql(parser.getSql(),page,size,parser.getParams());
			return pageModel;
		}finally{
			tool.closeConn();
		}
	}
	
	/**
	 * 查询对象
	 * @param c
	 * @param whereSql 过滤条件
	 * @return
	 * @throws Exception
	 */
	public PageModel<Map<String,Object>> queryPageSql(String talbeName,String whereSql,int page,int size,int dialect,String ... columns) throws SQLException{
		TableInfo info=new TableInfo(talbeName);
		info.setQueryFilds(columns);
		info.setFilterStr(whereSql);
		SQLRunner tool=null;
		try{
			tool=new SQLRunner(dataSource.getConnection());
			tool.setDbDialect(dialect);
			ParamParser parser=info.getSelectSQLParams();
			PageModel<Map<String,Object>> pageModel=tool.queryPageSql(parser.getSql(),page,size,parser.getParams());
			return pageModel;
		}finally{
			tool.closeConn();
		}
	}
	
	/**
	 * 查询对象
	 * @param c
	 * @param whereSql 过滤条件
	 * @return
	 * @throws Exception
	 */
	public PageModel<Map<String,Object>> queryPageSql(String sql,int page,int size,int dialect,Object ... params ) throws SQLException{
		SQLRunner tool=null;
		try{
			tool=new SQLRunner(dataSource.getConnection());
			tool.setDbDialect(dialect);
			PageModel<Map<String,Object>> pageModel=tool.queryPageSql(sql,page,size,params);
			return pageModel;
		}finally{
			tool.closeConn();
		}
	}
	/**
	 * 查询语句
	 * @param sql
	 * @param params
	 * @return
	 * @throws SQLException 
	 */
	public List<Map<String,Object>> query(String sql,Object ... params) throws SQLException{
		return DbUtils.query(dataSource.getConnection(), sql, params);
	}
	/**
	 * 查询语句
	 * @param sql
	 * @param params
	 * @return
	 * @throws SQLException 
	 */
	public List<Map<String,Object>> query(String sql,Map params) throws SQLException{
		ParamParser parser=new ParamParser(sql,params);
		return DbUtils.query(dataSource.getConnection(), parser.getSql(), parser.getParams());
	}
	/**
	 * 执行一条Sql
	 * @param sql
	 * @param params
	 * @return
	 * @throws SQLException
	 */
	public boolean execute(String sql,Object ... params) throws SQLException{
		return DbUtils.execute(dataSource.getConnection(), sql, params);
	}
	/**
	 * 执行更新
	 * @param sql
	 * @param params
	 * @return
	 * @throws SQLException
	 */
	public int executeUpdate(String sql,Object ... params) throws SQLException{
		return DbUtils.executeUpdate(dataSource.getConnection(), sql, params);
	}
	
	public DataSource getDataSource() {
		return dataSource;
	}
	/**
	 * 设置数据源
	 * @param dataSource
	 */
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
}
